<!DOCTYPE html>
<html lang="en">
<head>
    <meta charset="UTF-8">
    <meta http-equiv="X-UA-Compatible" content="IE=edge">
    <meta name="viewport" content="width=device-width, initial-scale=1.0">
    <title>Document</title>

</head>
<body>
    <div></div>
</body>
<script>

    class Event {
        constructor(){}
        handlers = {}
        addEventListen(type, handler){
            if(!(type in this.handlers)){
                this.handlers[type] = [];
            }
            this.handlers[type].push(handler);
        }
        removeEventListen(type, handler){
            if(!(type in this.handlers)){
                return new Error("没有这个事件可移除");
            }
            if(!handler){
                delete this.handlers[type];
            }else{
                const id = this.handlers[type].findIndex(ele => ele === handler)
                if(id === "undefined"){
                    return new Error("无该绑定事件");
                }
                this.handlers[type].splice(id,1);
                if(this.handlers[type].length == 0){
                    delete this.handlers[type];
                }
            }
        }
        dispatchEvent(type, ...argus){
            if(!(type in this.handlers)){
                return new Error("要触发的事件类型不存在！");
            }
            this.handlers[type].forEach(handler => {
                handler(...argus);
            });
        }
    }

    let event = new Event();
    function funevent1(name){
        console.log("funevent1",name);
    }
    function funevent2(name,age){
        console.log("funevent2",name,age);
    }
    event.addEventListen("console", funevent1);
    event.addEventListen("console", funevent2);

    // event.removeEventListen("console", funevent1);
    // event.removeEventListen("console");

    event.dispatchEvent("console", "jim","132");
    
</script>
</html>
